#include "sdk.h"
#include <wx/bitmap.h>
#include <wx/bmpbuttn.h>
#include <wx/checkbox.h>
#include <wx/sizer.h>
#include "configmanager.h"
#include "wx/things/toggle.h"
#include "wx/tglbtn.h"
#include "SearchInPanel.h"
#include "ThreadSearchControlIds.h"

SearchInPanel::SearchInPanel( wxWindow* parent, int id, const wxPoint& pos, const wxSize& size, long ( style ) ):
  wxPanel( parent, id, pos, size, wxTAB_TRAVERSAL ) {
  wxString prefix = ConfigManager::GetDataFolder() + _T( "/images/ThreadSearch/" );
  wxBitmapButton dummyBtn( this, wxID_ANY, wxBitmap( prefix + _T( "openfiles.png" ), wxBITMAP_TYPE_PNG ) );
  wxSize dummySize = dummyBtn.GetSize();
  m_pBtnSearchOpenFiles = new wxCustomButton( this, controlIDs.Get( ControlIDs::idBtnSearchOpenFiles ),
      wxBitmap( prefix + _T( "openfiles.png" ), wxBITMAP_TYPE_PNG ),
      wxDefaultPosition, dummySize );
  m_pBtnSearchOpenFiles->SetBitmapDisabled( wxBitmap( prefix + _T( "openfilesdisabled.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchOpenFiles->SetBitmapSelected( wxBitmap( prefix + _T( "openfilesselected.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchOpenFiles->SetMinSize( dummySize );
  m_pBtnSearchTargetFiles = new wxCustomButton( this, controlIDs.Get( ControlIDs::idBtnSearchTargetFiles ),
      wxBitmap( prefix + _T( "target.png" ), wxBITMAP_TYPE_PNG ),
      wxDefaultPosition, dummySize );
  m_pBtnSearchTargetFiles->SetBitmapDisabled( wxBitmap( prefix + _T( "targetdisabled.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchTargetFiles->SetBitmapSelected( wxBitmap( prefix + _T( "targetselected.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchTargetFiles->SetMinSize( dummySize );
  m_pBtnSearchProjectFiles = new wxCustomButton( this, controlIDs.Get( ControlIDs::idBtnSearchProjectFiles ),
      wxBitmap( prefix + _T( "project.png" ), wxBITMAP_TYPE_PNG ),
      wxDefaultPosition, dummySize );
  m_pBtnSearchProjectFiles->SetBitmapDisabled( wxBitmap( prefix + _T( "projectdisabled.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchProjectFiles->SetBitmapSelected( wxBitmap( prefix + _T( "projectselected.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchProjectFiles->SetMinSize( dummySize );
  m_pBtnSearchWorkspaceFiles = new wxCustomButton( this, controlIDs.Get( ControlIDs::idBtnSearchWorkspaceFiles ),
      wxBitmap( prefix + _T( "workspace.png" ), wxBITMAP_TYPE_PNG ),
      wxDefaultPosition, dummySize );
  m_pBtnSearchWorkspaceFiles->SetBitmapDisabled( wxBitmap( prefix + _T( "workspacedisabled.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchWorkspaceFiles->SetBitmapSelected( wxBitmap( prefix + _T( "workspaceselected.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchWorkspaceFiles->SetMinSize( dummySize );
  m_pBtnSearchDir = new wxCustomButton( this, controlIDs.Get( ControlIDs::idBtnSearchDirectoryFiles ),
                                        wxBitmap( prefix + _T( "folder.png" ), wxBITMAP_TYPE_PNG ),
                                        wxDefaultPosition, dummySize );
  m_pBtnSearchDir->SetBitmapDisabled( wxBitmap( prefix + _T( "folderdisabled.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchDir->SetBitmapSelected( wxBitmap( prefix + _T( "folderselected.png" ), wxBITMAP_TYPE_PNG ) );
  m_pBtnSearchDir->SetMinSize( dummySize );
  set_properties();
  do_layout();
}


BEGIN_EVENT_TABLE( SearchInPanel, wxPanel )

  EVT_TOGGLEBUTTON( controlIDs.Get( ControlIDs::idBtnSearchOpenFiles ), SearchInPanel::OnBtnClickEvent )
  EVT_TOGGLEBUTTON( controlIDs.Get( ControlIDs::idBtnSearchTargetFiles ), SearchInPanel::OnBtnSearchTargetFilesClick )
  EVT_TOGGLEBUTTON( controlIDs.Get( ControlIDs::idBtnSearchProjectFiles ), SearchInPanel::OnBtnSearchProjectFilesClick )
  EVT_TOGGLEBUTTON( controlIDs.Get( ControlIDs::idBtnSearchWorkspaceFiles ), SearchInPanel::OnBtnSearchWorkspaceFilesClick )
  EVT_TOGGLEBUTTON( controlIDs.Get( ControlIDs::idBtnSearchDirectoryFiles ), SearchInPanel::OnBtnClickEvent )

END_EVENT_TABLE();


void SearchInPanel::OnBtnClickEvent( wxCommandEvent &event ) {
  event.Skip();
}


void SearchInPanel::OnBtnSearchTargetFilesClick( wxCommandEvent &event ) {
  if( event.GetInt() ) {
    SetSearchInProjectFiles( false );
    SetSearchInWorkspaceFiles( false );
  }
  event.Skip();
}


void SearchInPanel::OnBtnSearchProjectFilesClick( wxCommandEvent &event ) {
  if( event.GetInt() ) {
    SetSearchInTargetFiles( false );
    SetSearchInWorkspaceFiles( false );
  }
  event.Skip();
}


void SearchInPanel::OnBtnSearchWorkspaceFilesClick( wxCommandEvent &event ) {
  if( event.GetInt() ) {
    SetSearchInTargetFiles( false );
    SetSearchInProjectFiles( false );
  }
  event.Skip();
}

void SearchInPanel::set_properties() {
  m_pBtnSearchOpenFiles->SetToolTip( _T( "在打开文件中搜索" ) );
  m_pBtnSearchTargetFiles->SetToolTip( _T( "在目标文件中搜索" ) );
  m_pBtnSearchProjectFiles->SetToolTip( _T( "在项目文件中搜索" ) );
  m_pBtnSearchWorkspaceFiles->SetToolTip( _T( "在工作空间文件中搜索" ) );
  m_pBtnSearchDir->SetToolTip( _T( "在目录文件中搜索" ) );
}

void SearchInPanel::do_layout() {
  wxBoxSizer* SizerTop = new wxBoxSizer( wxHORIZONTAL );
  SizerTop->Add( m_pBtnSearchOpenFiles, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL | wxADJUST_MINSIZE, 4 );
  SizerTop->Add( m_pBtnSearchTargetFiles, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL | wxADJUST_MINSIZE, 4 );
  SizerTop->Add( m_pBtnSearchProjectFiles, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL | wxADJUST_MINSIZE, 4 );
  SizerTop->Add( m_pBtnSearchWorkspaceFiles, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL | wxADJUST_MINSIZE, 4 );
  SizerTop->Add( m_pBtnSearchDir, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL | wxADJUST_MINSIZE, 4 );
  SetAutoLayout( true );
  SetSizer( SizerTop );
  SizerTop->Fit( this );
  SizerTop->SetSizeHints( this );
}

bool SearchInPanel::GetSearchInOpenFiles() const {return m_pBtnSearchOpenFiles->GetValue();}
bool SearchInPanel::GetSearchInTargetFiles() const {return m_pBtnSearchTargetFiles->GetValue();}
bool SearchInPanel::GetSearchInProjectFiles() const {return m_pBtnSearchProjectFiles->GetValue();}
bool SearchInPanel::GetSearchInWorkspaceFiles() const {return m_pBtnSearchWorkspaceFiles->GetValue();}
bool SearchInPanel::GetSearchInDirectory() const {return m_pBtnSearchDir->GetValue();}

void SearchInPanel::SetSearchInOpenFiles( bool bSearchInOpenFiles ) {
  m_pBtnSearchOpenFiles->SetValue( bSearchInOpenFiles );
}

void SearchInPanel::SetSearchInTargetFiles( bool bSearchInTargetFiles ) {
  m_pBtnSearchTargetFiles->SetValue( bSearchInTargetFiles );
}

void SearchInPanel::SetSearchInProjectFiles( bool bSearchInProjectFiles ) {
  m_pBtnSearchProjectFiles->SetValue( bSearchInProjectFiles );
}

void SearchInPanel::SetSearchInWorkspaceFiles( bool bSearchInWorkspaceFiles ) {
  m_pBtnSearchWorkspaceFiles->SetValue( bSearchInWorkspaceFiles );
}

void SearchInPanel::SetSearchInDirectory( bool bSearchInDirectoryFiles ) {
  m_pBtnSearchDir->SetValue( bSearchInDirectoryFiles );
}
